package com.example.mystoryapp2.ui.story.page

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.example.mystoryapp2.adapter.PageListAdapter
import com.example.mystoryapp2.contexts.AppContext
import com.example.mystoryapp2.contexts.StoryContext
import com.example.mystoryapp2.data.network.ServiceCreator
import com.example.mystoryapp2.data.network.model.PageModel
import com.example.mystoryapp2.domain.Page
import com.example.mystoryapp2.domain.RecordStatic
import com.example.mystoryapp2.domain.Records
import com.example.mystoryapp2.enums.EnumPageWorkMode
import com.example.mystoryapp2.ui.base.BaseListAdapter
import com.example.mystoryapp2.ui.base.BaseViewModel

class PageViewModel : BaseViewModel() {
    var adapter = MutableLiveData(PageListAdapter(ArrayList()))
    var page = MutableLiveData(Page())
    fun initPageList(listener: BaseListAdapter.IClickListener<Page>) {
        if (StoryContext.pageId == -1) {
            adapter.value?.setList(ArrayList())
            return
        }
        PageModel.getListByOId(StoryContext.pageId, object : ServiceCreator.ICallBack<List<Page>> {
            override fun onSuccess(res: List<Page>) {
                adapter.value?.setList(res as ArrayList<Page>)
                adapter.value?.setClickListener(listener)
            }
        })
    }

    /**
     * 根据StoryContext中的参数 跟新当前页的page 并且通过activity刷新 更新界面中显示的内容
     */
    fun initPage(p: () -> Unit) {
        if (StoryContext.pageId == -1) {
            p()
            return
        }
        PageModel.getById(StoryContext.pageId, object : ServiceCreator.ICallBack<Page> {
            override fun onSuccess(res: Page) {
                page.value = res
                //StoryContext.oriPageId = res.originId!!
                if (isRead()) {
                    recordRead()
                }

            }
        })
        PageModel.getParentId(StoryContext.pageId, object : ServiceCreator.ICallBack<Int> {
            override fun onSuccess(res: Int) {
                StoryContext.oriPageId = res
                p()
            }

            override fun onFailed() {
                p()
            }
        })
    }

    fun isFirst(): Boolean {
        return StoryContext.oriPageId == -1
    }

    fun isRead(): Boolean {
        return StoryContext.pageWorkMode == EnumPageWorkMode.READ
    }

    fun save(b: (Boolean) -> Unit) {
        page.value?.originId = StoryContext.oriPageId

        if (StoryContext.pageId != -1) {
            page.value?.let {
                PageModel.update(it, object : ServiceCreator.ICallBack<Boolean> {
                    override fun onSuccess(res: Boolean) {
                        b(res)
                    }
                })
            }
        } else {
            page.value?.storyId = StoryContext.storyId
            page.value?.pageId = null
            page.value?.let {
                PageModel.add(it, object : ServiceCreator.ICallBack<Int> {
                    override fun onSuccess(res: Int) {
                        if (res == -1) {
                            b(false)
                        } else {
                            StoryContext.pageId = res
                            page.value?.pageId = res
                            b(true)
                        }
                    }
                })
            }
        }
    }

    fun recordRead() {
        val records = Records(AppContext.userInfo.userId, page.value?.storyId, page.value?.pageId)
        PageModel.addRecords(records, object : ServiceCreator.ICallBack<Boolean> {
            override fun onSuccess(res: Boolean) {
                Log.d("pageView", "onSuccess: " + res)
            }
        })
    }
}